package com.health.service.impl;

import com.health.mapper.OrderSettingMapper;
import com.health.pojo.OrderSetting;
import com.health.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void add(List<OrderSetting> list) {
        if(list!=null&&list.size()>0){
            for(OrderSetting orderSetting:list){
                long countByOrderDate=orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if(countByOrderDate>0){
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                }else {
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin=date+"-1";
        String end=date+"-31";
        Map<String,String> map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list=orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> result=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(OrderSetting orderSetting:list){
                Map<String,Object> m=new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate=orderSetting.getOrderDate();
        long count=orderSettingMapper.findCountByOrderDate(orderDate);
        if(count>0){
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingMapper.add(orderSetting);
        }
    }
}
